package com.fire.control.system.config.getui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.fire.control.system.dto.GetuiDto;
import com.fire.control.system.support.util.JSONUtil;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;

@Component
public class GetuiManager {
	private static final Logger logger = LoggerFactory.getLogger(GetuiManager.class);

	@Value("${spring.getui.appId}")
	private String appId;
	@Value("${spring.getui.appKey}")
    private String appKey;
	@Value("${spring.getui.masterSecret}")
    private String masterSecret;
	@Value("${spring.getui.host}")
    private String host;
	
	
	/**
	* @Title: pushNotificationSingle
	* @Description: TODO(推送通知)
	* @author wanbo
	* @date  2017年7月11日 上午10:26:19
	* @param title
	* @param content
	* @param cid
	* @return
	*/
	public GetuiDto pushNotificationSingle(String title,String content,String cid){
		/*****************模板设置****************/
		NotificationTemplate template = getNotificationTemplate(title, content);
        /*****************推送设置****************/
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        //离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        //可选,1为wifi,0为不限制网络环境.根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0); 
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        GetuiDto dto = JSONUtil.toBean(ret.getResponse().toString(), GetuiDto.class);
        dto.setMsg(ret.getResponse().toString());
        return dto;
	}
	
	
	
	/**
	* @Title: pushLinkSingle
	* @Description: TODO(推送链接)
	* @author wanbo
	* @date  2017年7月11日 上午10:27:32
	* @param title
	* @param content
	* @param cid
	* @param url
	* @return
	*/
	public GetuiDto pushLinkSingle(String title,String content,String cid,String url){
		/*****************模板设置****************/
		LinkTemplate template = getLinkTemplate(title, content, url);
        /*****************推送设置****************/
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        //离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        //可选,1为wifi,0为不限制网络环境.根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0); 
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        GetuiDto dto = JSONUtil.toBean(ret.getResponse().toString(), GetuiDto.class);
        dto.setMsg(ret.getResponse().toString());
        return dto;
	}
	
	//通知模板
	private NotificationTemplate getNotificationTemplate(String title,String content){
		NotificationTemplate template = new NotificationTemplate();
		//设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        Style0 style = new Style0();
        //设置通知栏标题与内容
        style.setTitle(title);
        style.setText(content);
        //配置通知栏图标
        style.setLogo("icon.png");
        //配置通知栏网络图标
        style.setLogoUrl("");
        //设置通知是否响铃
        style.setRing(true);
        //震动
        style.setVibrate(true);        
        //可清除
        style.setClearable(true);	
        template.setStyle(style);
        //透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(1);
		return template;
	}
	
	//链接模板
	private LinkTemplate getLinkTemplate(String title,String content,String url){
		LinkTemplate template = new LinkTemplate();
		//设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        Style0 style = new Style0();
        //设置通知栏标题与内容
        style.setTitle(title);
        style.setText(content);
        //配置通知栏图标
        style.setLogo("icon.png");
        //配置通知栏网络图标
        style.setLogoUrl("");
        //设置通知是否响铃
        style.setRing(true);
        //震动
        style.setVibrate(true);        
        //可清除
        style.setClearable(true);	
        template.setStyle(style);
        //设置打开的网址地址
        template.setUrl(url);
		return template;
	}
	
	
	
}
